import { message } from 'antd';

const wrapMessage = () => {
    message.config({
        duration: 3,
    });
    return message;
}

export const messageNotice = wrapMessage();

/**判断value是否为空
 * @param {*} value String,Number,Object,Array均可以判断
 */
export function isEmptyValue(value) {
    let type;
    if (value == null) { // 等同于 value === undefined || value === null
        return false;
    }
    type = Object.prototype.toString.call(value).slice(8, -1);
    switch (type) {
        case 'String':
            return !value.length;
        case 'Array':
            return !value.length;
        case 'Object': {  // 普通对象使用 for...in 判断，有 key 即为 false
            for (let name in value) {
                return false;
            }
            return true;
        }
        default:
            return false; // 其他对象均视作非空
    }
}

/**
 * {Date}date
 */
export function dateToMysqlDate(date) {
    if (date.constructor != Date) {
        throw Error('Param date must be subClass of Date');
    }
    let year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours(),
        minute = date.getMinutes(),
        second = date.getSeconds();

    month = month > 9 ? month : '0' + month;
    day = day > 9 ? day : '0' + day;
    hour = hour > 9 ? hour : '0' + hour;
    minute = minute > 9 ? minute : '0' + minute;
    second = second > 9 ? second : '0' + second;
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}


export function genYearMonthDate() {
    let date = new Date();
    let year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate();

    month = month > 9 ? month : '0' + month;
    day = day > 9 ? day : '0' + day;
    return year + month + day;
}

/**
 * 先选中，再复制，可保证带格式
 * @param {Element} text .
 */
export function copyText(text) {
    if (document.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    } else {
        alert("浏览器不支持复制选中");
    }
    document.execCommand('Copy', 'false', null);
}

//取消选中
export function deSelect() {
    if (window.getSelection) {
        var selection = window.getSelection();
        selection.removeAllRanges();
    }
}

/**
 * 生成项目状态
 */

export function genPmsStatusName(pmsProStatus) {
    let transtatus = '暂无'
    switch (pmsProStatus) {
        case 0:
            transtatus = '开发中';
            break;
        case 1:
            transtatus = '提交测试';
            break;
        case 2:
            transtatus = '接手并且测试中';
            break;
        case 3:
            transtatus = '测试通过，待打Tag';
            break;
        case 4:
            transtatus = '已打Tag，待确认版本';
            break;
        case 5:
            transtatus = '已确认版本，待发布';
            break;
        case 6:
            transtatus = '已发布上线，待验证';
            break;
        case 7:
            transtatus = '发布成功';
            break;
    }
    return transtatus;
}


